//
//  MenuViewController.m
//  Project_B
//
//  Created by 张闯的mac pro on 15/11/30.
//  Copyright © 2015年 张闯. All rights reserved.
//
#define WIDTH [[UIScreen mainScreen] bounds].size.width
#define HEIGHT [[UIScreen mainScreen] bounds].size.height

#import "XPQScrollLabel.h"
#import "MenuViewController.h"
#import "FeedbackViewController.h"
#import "RESideMenu.h"
#import "AppDelegate.h"
#import "StoreViewController.h"
#import "CoreDataManager.h"
#import "UIImageView+WebCache.h"
#import "STKHTTPDataSource.h"
#import "ZCSTKPlayer.h"


@interface MenuViewController ()

@property (nonatomic, strong) CoreDataManager *coreDataManager;

@property (nonatomic, retain) UIButton *playButton;
@property (nonatomic, retain) UIButton *upButton;
@property (nonatomic, retain) UIButton *nextButton;
@property (nonatomic, retain) STKDataSource * dataSource;
@property (nonatomic, retain) NSMutableArray * musarr;
@property (nonatomic, assign) NSInteger i;
@property (nonatomic, retain) STKAudioPlayer * player;
@property (nonatomic, retain) UILabel * titles;

@end

@implementation MenuViewController

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"音乐1" object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"音乐" object:@"pause"];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"音乐" object:@"play"];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor clearColor];
    UIImageView *backImg = [[UIImageView alloc] initWithFrame:self.view.frame];
    [self.view addSubview:backImg];
    [backImg setImage:[UIImage imageNamed:@"22.png"]];
   
    [self creatButtons];
    
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    [center addObserver:self selector:@selector(changePlayButton:) name:@"音乐1" object:nil];
    
}

- (void)changePlayButton:(NSNotification *)note
{
    if ([note.object isEqualToString:@"play"]) {
        [self.playButton setBackgroundImage:[UIImage imageNamed:@"playButton"] forState:0];
    } else if ([note.object isEqualToString:@"pause"]) {
        [self.playButton setBackgroundImage:[UIImage imageNamed:@"pauseButton"] forState:0];
    }
}


#pragma mark -创建侧栏tableView
- (void)creatButtons
{
    UIButton *storeBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    storeBtn.frame = CGRectMake((WIDTH - 130) / 3, 350, 100, 30);
    [self.view addSubview:storeBtn];
    [storeBtn.titleLabel setFont:[UIFont systemFontOfSize:21]];
    [storeBtn setTitle:@"我的收藏" forState:0];
    [storeBtn addTarget:self action:@selector(storeAction) forControlEvents:UIControlEventTouchUpInside];
    storeBtn.tintColor = [UIColor whiteColor];
    
    UIButton *backMessageBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    backMessageBtn.frame = CGRectMake((WIDTH - 130) / 3, 250, 100, 30);
    [self.view addSubview:backMessageBtn];
    [backMessageBtn.titleLabel setFont:[UIFont systemFontOfSize:21]];
    [backMessageBtn setTitle:@"用户反馈" forState:0];
    backMessageBtn.tintColor = [UIColor whiteColor];
    [backMessageBtn addTarget:self action:@selector(feedbackAction) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *clearBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    clearBtn.frame = CGRectMake((WIDTH - 130) / 3, 300, 100, 30);
    [self.view addSubview:clearBtn];
    [clearBtn.titleLabel setFont:[UIFont systemFontOfSize:21]];
    [clearBtn setTitle:@"清除缓存" forState:0];
    [clearBtn addTarget:self action:@selector(clearAction) forControlEvents:UIControlEventTouchUpInside];
    clearBtn.tintColor = [UIColor whiteColor];
    
    UIButton *nightBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    nightBtn.frame = CGRectMake((WIDTH - 130) / 3, 350, 100, 30);
   // [self.view addSubview:nightBtn];
    [nightBtn.titleLabel setFont:[UIFont systemFontOfSize:21]];
    [nightBtn setTitle:@"夜间模式" forState:0];
    nightBtn.tintColor = [UIColor whiteColor];
    
    UIButton *mainBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    mainBtn.frame = CGRectMake((WIDTH - 130) / 3, 200, 100, 30);
    [self.view addSubview:mainBtn];
    [mainBtn setTitle:@"返回主页" forState:0];
    [mainBtn.titleLabel setFont:[UIFont systemFontOfSize:21]];
    [mainBtn setTitleColor:[UIColor whiteColor] forState:0];
    [mainBtn addTarget:self action:@selector(mainAction) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *onlineBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    onlineBtn.frame = CGRectMake((WIDTH - 130) / 3, 400, 100, 30);
   // [self.view addSubview:onlineBtn];
    [onlineBtn setTitle:@"在线客服" forState:0];
    [onlineBtn.titleLabel setFont:[UIFont systemFontOfSize:21]];
    [onlineBtn setTitleColor:[UIColor whiteColor] forState:0];
   // [onlineBtn addTarget:self action:@selector(onlineAction) forControlEvents:UIControlEventTouchUpInside];
    
    self.player = [ZCSTKPlayer shareInstance];
    
    //播放
    self.playButton = [UIButton buttonWithType:UIButtonTypeSystem];
    self.playButton.frame = CGRectMake((WIDTH - 50) / 3 , HEIGHT / 1.5 + 20, 50, 50);
    [self.playButton setBackgroundImage:[UIImage imageNamed:@"pauseButton"] forState:UIControlStateNormal];
    
    if (([self.player state] == STKAudioPlayerStatePaused)) {
        [self.playButton setBackgroundImage:[UIImage imageNamed:@"playButton"] forState:UIControlStateNormal];
    } else {
        [self.playButton setBackgroundImage:[UIImage imageNamed:@"pauseButton"] forState:UIControlStateNormal];
    }
    
    [self.view addSubview:self.playButton];
    [self.playButton addTarget:self action:@selector(playButtonAction) forControlEvents:UIControlEventTouchUpInside];//button触发事件
    
    
    self.titles = [[UILabel alloc] initWithFrame:CGRectMake((WIDTH - 220) / 3, 400, 170, 50)];
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    [center addObserver:self selector:@selector(setTopTitle:) name:@"top" object:nil];
    self.titles.textAlignment = NSTextAlignmentCenter;
    self.titles.numberOfLines = 0;
    self.titles.backgroundColor = [UIColor clearColor];
    self.titles.textColor = [UIColor whiteColor];
    self.titles.font = [UIFont fontWithName:@"Helvetica-Bold" size:15];
    [self.view addSubview:self.titles];
    
}

- (void)setTopTitle:(NSNotification *)note
{
    self.titles.text = note.object;
}

- (void)storeAction
{
    [self.sideMenuViewController setContentViewController:[[UINavigationController alloc] initWithRootViewController:[[StoreViewController alloc] init]]animated:YES];
    [self.sideMenuViewController hideMenuViewController];
}


- (void)mainAction
{
    AppDelegate *app = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    UITabBarController *tab = app.tab;
    
    [self.sideMenuViewController setContentViewController:tab animated:YES];
    [self.sideMenuViewController hideMenuViewController];
}


- (void)clearAction
{
    NSLog(@"清楚缓存");
    
    NSString *str = [NSString stringWithFormat:@"要清除么?"];
    
    UIAlertController *alertContrl = [UIAlertController alertControllerWithTitle:@"提示信息" message:str preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *sure = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        
        [self clearCache];
        
    }];
    
    UIAlertAction *cancle = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    [alertContrl addAction:sure];
    [alertContrl addAction:cancle];
    [self presentViewController:alertContrl animated:YES completion:nil];
}


- (void)feedbackAction
{
    [self.sideMenuViewController setContentViewController:[[UINavigationController alloc] initWithRootViewController:[[FeedbackViewController alloc] init]]animated:YES];
    [self.sideMenuViewController hideMenuViewController];
}

- (CGFloat)getCacheSize
{
    NSString * path = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).lastObject;
    NSDictionary * dict = [[NSFileManager defaultManager] attributesOfItemAtPath:path error:nil];
    NSString *str = [dict objectForKey:NSFileSize];
    CGFloat size = [str floatValue];

    return size;
}

- (void)clearCache
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *caches = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).lastObject;
    [fileManager removeItemAtPath:caches error:nil];
    
    [[SDImageCache sharedImageCache] clearDisk];
    [[SDImageCache sharedImageCache] clearMemory];
}

- (NSMutableArray *)selectVideoModel
{
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Video" inManagedObjectContext:self.coreDataManager.managedObjectContext];
    [fetchRequest setEntity:entity];
    // Specify criteria for filtering which objects to fetch
//    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"title = %@", [self.modelArr[self.i] title]];
//    [fetchRequest setPredicate:predicate];
    // Specify how the fetched objects should be sorted
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"title"
                                                                   ascending:YES];
    [fetchRequest setSortDescriptors:[NSArray arrayWithObjects:sortDescriptor, nil]];
    
    NSError *error = nil;
    NSArray *fetchedObjects = [self.coreDataManager.managedObjectContext executeFetchRequest:fetchRequest error:&error];
    
    NSMutableArray *arr = [NSMutableArray arrayWithArray:fetchedObjects];
    
    if (fetchedObjects == nil) {
        return nil;
    }
    return arr;
}


- (void)playButtonAction
{
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    if ([self.player state] == STKAudioPlayerStatePaused) {
        //注册通知
        
        [center postNotificationName:@"音乐" object:@"pause" userInfo:nil];
        [self.playButton setBackgroundImage:[UIImage imageNamed:@"pauseButton"] forState:0];
        [[self player] resume];
        
    } else if ([[self player] state] == STKAudioPlayerStatePlaying) {
        
        [center postNotificationName:@"音乐" object:@"play" userInfo:nil];
        [self.playButton setBackgroundImage:[UIImage imageNamed:@"playButton"] forState:0];
        [[self player] pause];
        
    } else {
        
        [self.playButton setBackgroundImage:[UIImage imageNamed:@"pauseButton"] forState:UIControlStateNormal];
    }

}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
